#!/bin/sh
# shellcheck shell=dash disable=SC2169
#
# Starts ssdpd
#

DAEMON="ssdpd"
PIDFILE="/var/run/${DAEMON}.pid"

start() {
	echo -n "Starting ${DAEMON}: "

	# adjust the oom score (which is inherited by start-stop-daemon)
	# to ensure that others are killed first in case of low memory situations
	echo -900 >/proc/$$/oom_score_adj 2> /dev/null

	if start-stop-daemon -S -q -b -m -c ssdp:ssdp -p ${PIDFILE} --exec /bin/${DAEMON}; then
		echo "OK"
	else
		echo "FAIL"
	fi
}
stop() {
	echo -n "Stopping ${DAEMON}: "
	if start-stop-daemon -K -q -p ${PIDFILE}; then
		echo "OK"
	else
		echo "FAIL"
	fi
	rm -f ${PIDFILE}
}
restart() {
	stop
	start
}

case "$1" in
	start)
		start
	;;
	stop)
		stop
	;;
	restart|reload)
		restart
	;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
esac

exit 0
